package com.springcloudalibaba.serviceUser.controller.buyer;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.mysql.jdbc.log.LogUtils;
import com.springcloudalibaba.basefeign.service.order.api.OrderFeign;
import com.springcloudalibaba.basefeign.service.order.vo.BuyerOrderVo;
import com.springcloudalibaba.basefeign.service.product.api.ProductFeign;
import com.springcloudalibaba.basefeign.service.product.vo.SellerProductVo;
import com.springcloudalibaba.bclass.base.BaseException;
import com.springcloudalibaba.bclass.base.BaseResult;
import com.springcloudalibaba.bclass.enums.SysStateEnum;
import com.springcloudalibaba.bclass.util.LoggerUtil;
import com.springcloudalibaba.serviceUser.entity.buyer.BuyerEntity;
import com.springcloudalibaba.serviceUser.entity.seller.SellerEntity;
import com.springcloudalibaba.serviceUser.service.buyer.BuyerService;
import com.springcloudalibaba.serviceUser.service.seller.SellerService;
import io.seata.core.context.RootContext;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;

/**
 * <p>
 * 买家信息 前端控制器
 * </p>
 *
 * @author lixing_java@163.com
 * @since 2021-11-27
 */
@RestController
@RequestMapping("/buyer")
public class BuyerController {
    @Resource
    private BuyerService buyerService;

    @Resource
    private SellerService sellerService;

    @Resource
    private ProductFeign productFeign;

    @Resource
    private OrderFeign orderFeign;

    /**
     * @描述 创建订单
     * @作者 lixing_java@163.com
     * @日期 2021/9/17 14:29
     */
    @PostMapping("createOrder")
    @GlobalTransactional
    public BaseResult<String> createOrder() {
        System.out.println("xid========================" + RootContext.getXID());
        // 扣库存（分布式事务）
        SellerProductVo sellerProductVo = new SellerProductVo();
        sellerProductVo.setId(1000000L);
        sellerProductVo.setStock(1);
        BaseResult<String> baseResultForChangeStock = productFeign.changeStock(sellerProductVo);
        if (!baseResultForChangeStock.isSuccess()) {
            throw new BaseException(baseResultForChangeStock.getMsg());
        }

        // 创建订单（分布式事务）
        BuyerOrderVo buyerOrderVo = new BuyerOrderVo();
        buyerOrderVo.setCreateTime(new Date());
        buyerOrderVo.setCode("order1000000");
        buyerOrderVo.setProductId(1000000L);
        buyerOrderVo.setPayState(SysStateEnum.ORDER_STATE_ING.getValue());
        BaseResult<String> baseResultForCreateOrder = orderFeign.createOrder(buyerOrderVo);
        if (!baseResultForCreateOrder.isSuccess()) {
            throw new BaseException(baseResultForChangeStock.getMsg());
        }

        // 扣减账户金额（本地事务）
        boolean updateBuyerFlag = buyerService.update(new UpdateWrapper<BuyerEntity>().set("grade", 0).eq("account", "31011225"));
        if (!updateBuyerFlag) {
            throw new BaseException("扣减账户金额失败");
        }
        //
        System.out.println(0/0);
        return new BaseResult<String>().success(buyerOrderVo.getCode());
    }
}

